﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CharitiesLib;
using System.IO;
using Charities.ModelViews;

namespace Charities.Forms
{
    public partial class frmPersonReports : Form
    {
        public frmPersonReports(int pid , Form mdi)
        {
            MdiParent = mdi;
            id = pid;
            InitializeComponent();
        }
        int id;
        ModelContainer ctx = new ModelContainer();
        private void frmPersonReports_Load(object sender, EventArgs e)
        {
            Person per = ctx.People.Single(p => p.Id == id);
            Worthy wor = ctx.Worthies.Single(w => w.Id == per.WorthyId);
            Person wper = ctx.People.Single(p => p.Id == wor.PersonId);
            PersonVM pv = new PersonVM();
            pv.Id = wor.Id;
            pv.Amana = per.Amana;
            pv.DOB = per.DOB.Value.ToShortDateString();
            pv.FatherName = per.FatherName;
            pv.FirstName = per.FirstName;
            pv.HealthStatus = per.HealthStatus;
            pv.Job = per.JobTitle;
            pv.LastName = per.LastName;
            pv.MotherName = per.MotherName;
            pv.NationalNumber = per.NationalNumber;
            pv.PinctuePath = per.PersonalPicture;
            pv.POP = per.POB;
            pv.QaidNumber = per.Qaid;
            pv.WorthyDisplayName = wper.FirstName + " " + wper.FatherName + " " + wper.LastName;
            pv.WorthyType = wor.WorthyType.Name;
            pv.CurrenntAddress = wor.CurrentAddress;
            pv.OriginalAddress = wor.OriginalAddress;
            pv.RelationWithWorthy = per.RelationshipWithWorthy;
            pv.PhoneNumber = wor.Phone;
            if (per.PersonalPicture != null)
            {
                pv.PinctuePath = per.PersonalPicture;
            }
            PersonVMBindingSource.DataSource = pv;
            if (ctx.CharityInfos.Count() > 0)
            {
                reportViewer1.LocalReport.EnableExternalImages = true;
                CharityInfoBindingSource.DataSource= ctx.CharityInfos.First();
            }
            else
            {
                MessageBox.Show("يجب تنصيب الاعدادات الرئيسية حتى تعمل التقارير بالشكل الامثل ");
            }
            this.reportViewer1.RefreshReport();

        }
        public byte[] imageToByteArray(System.Drawing.Image imageIn)
        {
            MemoryStream ms = new MemoryStream();
            imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
            return ms.ToArray();
        }
        public static Image LoadImage(byte[] imageBytes)
        {
            Image image = null;
            using (var ms = new MemoryStream(imageBytes))
                image = Image.FromStream(ms);

            return image;
        }
    }


}

